<?php


require_once 'doctrine-orm/Doctrine/Common/Persistence/ObjectRepository.php';
require_once 'doctrine-orm/Doctrine/ORM/EntityRepository.php';

use Doctrine\ORM\EntityRepository,
    Doctrine\Common\Persistence\ObjectRepository;

/**
 * Description of GameRepository
 *
 * @author alexey.ogarkov
 */
class GameRepository extends EntityRepository{
    
    public function createGame(Player $player,$game) {
        $persist_game = new Game();
        
        $id = substr(md5(uniqid(rand(), true)), 0, 20);
        
        $persist_game->setId($id);
        
        $persist_game->setOwner($this->getEntityManager()->merge($player));
        $persist_game->setOnline($game->online);
        $persist_game->setStatus("WAITING");
        $cur_time = new DateTime();
        $persist_game->setStartDate($cur_time);
        $persist_game->setUpdateDate($cur_time);
        
        $update_hash = substr(md5(uniqid($cur_time->getTimestamp(), true)), 0, 20);
        $persist_game->setUpdateHash($update_hash);                     
        
        $this->getEntityManager()->persist($persist_game);
        $this->getEntityManager()->flush();
        return $persist_game;
    }
}

?>
